# GRAPHING/PLOTTING CONVENIENCE FUNCTIONS --------------------------------------

# function for brackets in a ggplot
# note that units of brackets "npc", the only unit (besides physical units)
# that makes sense when annotating the plot panel in ggplot2 (since we have
# no access to native units
BracketsGrob <- function(...) {
    l <- list(...)
    e <- new.env()
    e$l <- l
    grid:::recordGrob(
        {
            do.call(grid.brackets, l)
        },
        e
    )
}

# function for event study plots with two y axes
PlotEventStudyLineTwoY <-
    function(figdata,
             second_y_var,
             event_time_var,
             lower_event = -7,
             upper_event = 5,
             omitted_event_time = -2,
             ci_factor = 1.64,
             scale_factor = 1,
             base_size = 9,
             xlab_text = "Event Time (ℓ)",
             ylab_text_left = "Event Study Estimate",
             ylab_text_right = "% Change (relative to pre)",
             y_lower,
             y_upper,
             breaks_main_y = 10,
             break_width_second_y) {

        # As this function changes the underlying data
        # and figdata tends to be a small file, make a copy now
        figdata_temp <- copy(figdata)

        # Construct rel_estimate and fix both to be on that scale
        max_treat_mean <- figdata_temp[, max(get(second_y_var), na.rm = TRUE)]
        figdata_temp[
            ,
            rel_estimate := (estimate / get(second_y_var)) * max_treat_mean
        ]
        figdata_temp[get(event_time_var) == omitted_event_time, rel_estimate := 0]

        min_y <-
            min(
                figdata_temp[
                    ,
                    min(
                        ((estimate - (ci_factor * cluster_se)) * scale_factor),
                        na.rm = TRUE
                    )
                ],
                figdata_temp[, min(rel_estimate, na.rm = TRUE)]
            )
        max_y <-
            max(
                figdata_temp[
                    ,
                    max(
                        ((estimate + (ci_factor * cluster_se)) * scale_factor),
                        na.rm = TRUE
                    )
                ],
                figdata_temp[, max(rel_estimate, na.rm = TRUE)]
            )

        fig <-
            ggplot(
                data = figdata_temp,
                aes(x = event_time, y = (estimate * scale_factor))
            ) +
            geom_line() +
            geom_ribbon(
                aes(
                    ymin = (estimate - (ci_factor * cluster_se)) * scale_factor,
                    ymax = (estimate + (ci_factor * cluster_se)) * scale_factor
                ),
                linetype = 0,
                alpha = 0.2,
                show.legend = FALSE
            ) +
            geom_line(aes(y = rel_estimate), alpha = 0) +
            theme_bw(base_size = base_size) +
            theme(panel.grid.minor = element_blank()) +
            scale_x_continuous(
                breaks = seq.int(from = lower_event, to = upper_event, by = 1),
                expand = expansion(mult = c(0.0025, 0.0025))
            ) +
            scale_y_continuous(
                breaks = breaks_extended(n = breaks_main_y),
                limits = c(min_y, max_y),
                name = ylab_text_left,
                sec.axis = sec_axis(~ . * 100 / (max_treat_mean * scale_factor),
                    name = sprintf("%s", ylab_text_right),
                    breaks = breaks_width(width = break_width_second_y)
                )
            ) +
            labs(x = xlab_text)

        if (lower_event <= omitted_event_time) {
            fig <- fig + coord_cartesian(ylim = c(y_lower, y_upper))
        }

        if (lower_event > omitted_event_time) {
            fig <-
                fig +
                coord_cartesian(
                    xlim = c(lower_event, upper_event),
                    ylim = c(y_lower, y_upper)
                )
        }

        return(fig)
    }